﻿using System;

namespace b5
{
    public abstract class IR
    {
        //返回面积
        public abstract double showS();
        //检查合法性
        public abstract bool IsRight();
    }


    class rectangle : IR
    {
        public rectangle(double a, double b)
        {
            length = a;
            width = b;
        }

        private double length;
        private double width;

        public override double showS()
        {
            if (!IsRight())
            {
                Console.WriteLine("此长方形为非法图形");
                return 0;
            }
            return length * width;
        }
        public override bool IsRight()
        {
            if ((length <= 0) || (width <= 0))
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
    class square : IR
    {
        public square(double a)
        {
            side = a;
        }
        private double side;
        public override double showS()
        {
            if (!IsRight())
            {
                Console.WriteLine("此正方形为非法图形");
                return 0;
            }
            return side * side;
        }
        public override bool IsRight()
        {
            if (side <= 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


    }
    class triangle : IR
    {
        public triangle(double a, double b, double c)
        {
            length1 = a;
            length2 = b;
            length3 = c;
        }

        private double length1;
        private double length2;
        private double length3;

        public override bool IsRight()
        {
            if ((length1 <= 0) || (length2 <= 0) || (length3 <= 0))
            {
                return false;
            }
            else if ((length1 + length2 <= length3) || (length2 + length3 <= length1) || (length1 + length3 <= length2))
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public override double showS()
        {
            if (!IsRight())
            {
                Console.WriteLine("此三角形为非法图形");
                return 0;
            }
            return Math.Sqrt((length1 + length2 + length3) * (length1 + length2 - length3)
                * (length1 + length3 - length2) * (length2 + length3 - length1)) / 4;
        }
    }
    
    
    class factory
    {
        public IR getIR(int i,Random t)
        {
            IR ir = null;
            //长方形
            if (i == 1)
            {
                double a = 0;
                double b = 0;
                ir =new rectangle(a, b);
                while (!ir.IsRight())
                {
                    a = Convert.ToDouble(Convert.ToInt32(t.NextDouble() * 100)) / 10;
                    b = Convert.ToDouble(Convert.ToInt32(t.NextDouble() * 100)) / 10;
                    ir = new rectangle(a, b);
                }
                Console.WriteLine("生成了长为" + a + "宽为" + b+"的长方形");
            }else if (i == 2)
            {//正方形
                double a = 0;
                ir = new square(a);
                while (!ir.IsRight())
                {
                    a = Convert.ToDouble(Convert.ToInt32(t.NextDouble() * 100)) / 10;
                    ir = new square(a);
                }
                Console.WriteLine("生成了边长为" + a + "的正方形");
            }else if (i == 3)
            {
                double a = 0;
                double b = 0;
                double c = 0;
                ir = new triangle(a, b, c);
                while (!ir.IsRight())
                {
                    a = Convert.ToDouble(Convert.ToInt32(t.NextDouble() * 100)) / 10;
                    b = Convert.ToDouble(Convert.ToInt32(t.NextDouble() * 100)) / 10;
                    c = Convert.ToDouble(Convert.ToInt32(t.NextDouble() * 100)) / 10;
                    ir = new triangle(a, b, c);
                }
                Console.WriteLine("生成了三边分别为" + a + "," + b + "," + c + "的三角形");
            }
            return ir;
        }
    }
    
    
    
    class Program
    {
        static void Main(string[] args)
        {
            factory fac = new factory();
            Random t = new Random();
            int mod = 0;
            mod = t.Next(1, 4);
            IR p1 = fac.getIR(mod,t);
            mod = t.Next(1, 4);
            IR p2 = fac.getIR(mod, t);
            mod = t.Next(1, 4);
            IR p3 = fac.getIR(mod, t);
            mod = t.Next(1, 4);
            IR p4 = fac.getIR(mod, t);
            mod = t.Next(1, 4);
            IR p5 = fac.getIR(mod, t);
            mod = t.Next(1, 4);
            IR p6 = fac.getIR(mod, t);
            mod = t.Next(1, 4);
            IR p7 = fac.getIR(mod, t);
            mod = t.Next(1, 4);
            IR p8 = fac.getIR(mod, t);
            mod = t.Next(1, 4);
            IR p9 = fac.getIR(mod, t);
            mod = t.Next(1, 4);
            IR p10 = fac.getIR(mod, t);

            Console.WriteLine("随机生成的图形面积和为：" + (p1.showS() + p2.showS() + p3.showS() + p4.showS() + p5.showS() + p6.showS() + p7.showS() + p8.showS() + p9.showS() + p10.showS()));


        }
    }
}
